package com.test.demo.network;

import com.apkfuns.logutils.LogUtils;
import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.test.demo.BuildConfig;


import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

public class CustomizeGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private final Gson gson;
    private final TypeAdapter<T> adapter;

    CustomizeGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        //把responsebody转为string
//        String response = AESUtils.decrypt(value.string());
        if (BuildConfig.DEBUG) {
            LogUtils.e(value.toString());
        }

        // 这里只是为了检测code是否!=1,所以只解析HttpStatus中的字段,因为只要code和message就可以了
        try {
            return adapter.fromJson(value.toString());
        } finally {
            value.close();
        }
    }
}
